package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.mall.lib.dto.ProductDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by zhangjh on 2016/10/24.
 */
@Repository
public class ProductShareRepositoryImpl implements ProductShareRepositoryCustom {
    private static final Logger log = LoggerFactory.getLogger(ProductShareRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public PageResult<ProductDto> getCustomerShareList(Long customerId, PageResult pageResult){
        SQLPageDoing<ProductDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s FROM product_share ps LEFT JOIN product p ON p.id = ps.product_id WHERE ps.shared_customer_id = :customerId " );
            sql.append(" AND ps.is_delete = \"NO\" AND p.is_delete = \"NO\" ORDER BY ps.last_updated DESC");
            fields.append("p.*");
            params.put("customerId",customerId);
        };
        return doing.go(pageResult, ProductDto.class, objectMapper , jdbcTemplate, log);
    }
}
